package com.yonyou.pmclouds.plan.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.PlanProgressVO;
import com.yonyou.pmclouds.plan.entity.ScheduleReportVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;
import com.yonyou.pmclouds.plan.mapper.PlanProgressMapper;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressHistoryService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressQueryService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleProgressService.class)
public class ScheduleProgressServiceImpl implements IScheduleProgressService {

    @Autowired
    private PlanProgressMapper progressMapper;

    @Autowired
    private AuditInfoUpdateProcessor<PlanProgressVO> updateProcessor;

    @Autowired
    private AuditInfoInsertMultiProcessor<PlanProgressVO> infoInsertMultiProcessor;

    @Autowired
    private IScheduleProgressHistoryService historyService;

    @Autowired
    private IScheduleProgressQueryService progressQueryService;

    @Override
    public PlanProgressVO[] insert(PlanProgressVO[] progressVOS) throws BusinessException {
        if(progressVOS == null || progressVOS.length <= 0){
            return null;
        }

        MultiInsertBpTemplate<PlanProgressVO> insertBpTemplate = new MultiInsertBpTemplate<>(progressMapper, false);
        insertBpTemplate.addBeforeProcessor(infoInsertMultiProcessor);

        return insertBpTemplate.insert(progressVOS);
    }

    @Override
    public PlanProgressVO[] delete(PlanProgressVO[] progressVOS) throws BusinessException {
        if(progressVOS == null || progressVOS.length <= 0){
            return null;
        }

        SingleDeleteBpTemplate<PlanProgressVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(progressMapper);
        deleteBpTemplate.addBeforeProcessor(updateProcessor);
        for (PlanProgressVO progressVO : progressVOS) {
            deleteBpTemplate.delete(progressVO);
        }

        return progressVOS;
    }

    @Override
    public PlanProgressVO[] update(PlanProgressVO[] progressVOS) throws BusinessException {
        if(progressVOS == null || progressVOS.length <= 0){
            return null;
        }

        SingleUpdateBpTemplate<PlanProgressVO> updateBpTemplate = new SingleUpdateBpTemplate<>(progressMapper, new String[]{
                PlanProgressVO.PROGRESS, PlanProgressVO.START_DATE, PlanProgressVO.END_DATE, PlanProgressVO.LAST_SUBMIT_DATE,
                PlanProgressVO.FIRST_SUBMIT_DATE
        });
        updateBpTemplate.addBeforeProcessor(updateProcessor);

        for(PlanProgressVO progressVO : progressVOS){
            updateBpTemplate.update(progressVO);
        }

        return progressVOS;
    }

    @Override
    public ScheduleReportVO updateProgress(ScheduleReportVO reportVO) throws BusinessException {

        List<PlanProgressVO> insertProgressList = new ArrayList<>();
        List<PlanProgressVO> updateProgressList = new ArrayList<>();
        PlanProgressVO[] oldProgressVOS = progressQueryService.queryByPkItems(new String[]{reportVO.getPkScheduleItem()});
        if(oldProgressVOS == null || oldProgressVOS.length <= 0){
            insertProgressList.add(reportVO.getPlanProgressVO());
        }else {
            updateProgressList.add(reportVO.getPlanProgressVO());
        }

        if(insertProgressList.size() > 0){
            this.insert(insertProgressList.toArray(new PlanProgressVO[]{}));
        }

        if(updateProgressList.size() > 0){
            this.update(updateProgressList.toArray(new PlanProgressVO[]{}));
        }

        historyService.updateProgress(reportVO);

        return reportVO;
    }
}
